﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LiveShopvnext.SKU
{
    public class SKUService : ApplicationService, ISKUAppService
    {

        #region 依赖注入
        private readonly IRepository<Specifica, int> _specifica; //sku
        private readonly IRepository<SpecificaSku, int> _specificaSku; //详情

        public SKUService(
                IRepository<Specifica, int> specifica,
                IRepository<SpecificaSku, int> specificaSku
            )
        {
            _specifica = specifica;
            _specificaSku = specificaSku;
        }
        #endregion

        #region 添加 规格详情
        /// <summary>
        /// 规格详情
        /// </summary>
        /// <param name="specificaDto"></param>
        /// <returns></returns>
        [HttpPost]
        public string specificaAdd(List<CreateUpdateSpecificaDto> specificaDto)
        {
            //雪花id
            string Snowflake = new IdWorker(1, 1).NextId().ToString();

            List<Specifica> specificas = new List<Specifica>();

            foreach (var item in specificaDto)
            {
                specificas.Add(new Specifica
                {
                    productId=item.childProductId, //产品id
                    SpecificaNo= Snowflake, //规格编号
                    Specificaparameter=item.childProductSpec, //规格
                    SpecificaSalePrice=item.childProductCost, //原价
                    SpecificaPromotion=item.childProductPrice, //销售价格
                    SpecificaSum=item.childProductStock, //数量
                    isUse=item.isUse //是否使用
                });
            }

            _specifica.InsertManyAsync(specificas);

            return "添加成功";
            
        }
        #endregion

        #region 添加 规格sku
        /// <summary>
        /// 规格sku
        /// </summary>
        /// <param name="SepcificaSkuDto"></param>
        /// <returns></returns>
        [HttpPost]
        public string specificaSku(List<CreateUpdateSepcificaSkuDto> SepcificaSkuDto)
        {
            List<SpecificaSku> sepcificaSkus= new List<SpecificaSku>();

            foreach (var item in SepcificaSkuDto)
            {
                sepcificaSkus.Add(new SpecificaSku
                {
                    productId = item.childProductId, //产品id
                    SpecificaSkuName = item.name,//规格名称
                    SpecificaSkuparameter = string.Join(',',item.value)
                });
            }

            _specificaSku.InsertManyAsync(sepcificaSkus);

            return "添加成功";
        }
        #endregion

        ///// <summary>
        ///// 展示sku
        ///// </summary>
        ///// <returns></returns>
        //public List<SepcificaSkuListDto> GetsepcificaSku()
        //{
            
        //}

    }
}
